﻿@{
    ViewData["Title"] = "通知公告列表";
}
<div class="layui-layout layui-layout-admin">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="title" autocomplete="off" class="layui-input">
                    </div>
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-inline">
                        <select name="type">
                            <option value="" selected>所有</option>
                            <option value="1">通知</option>
                            <option value="2">公告</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn" lay-submit lay-filter="btnsearch">
                            <i class="layui-icon layui-icon-search"></i> 查询
                        </button>
                    </div>
                </div>
            </form>

            <table id="lsttable" lay-filter="lsttable"></table>
            <script type="text/html" id="toolbar">
                <button class="layui-btn layui-btn-sm" lay-event="add">
                    <i class="layui-icon layui-icon-addition"></i> 添加
                </button>
            </script>
            <script type="text/html" id="bar">
                <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del">删除</a>
            </script>
        </div>
    </div>
</div>

@section scripts {
    <script type="text/javascript">
        var app = {};
        layui.use(['tools', 'toast', 'http'], function () {
            var table = layui.table,
                $ = layui.$,
                form = layui.form,
                http = layui.http,
                toast = layui.toast,
                tools = layui.tools;

            app.init = function () {
                //
                form.on('submit(btnsearch)', function (data) {
                    console.log(data.field)
                    table.reload('lsttable', {
                        where: data.field
                    })
                    return false;
                });
                //
                let cols =
                    [[
                        { checkbox: true, fixed: true },
                        { title: '标题', field: 'title' },
                        {
                            title: '类型', field: 'type', align: 'center', width: 66, templet: function (d) {
                                if (d.type === 1) {
                                    return '<span class="layui-badge layui-bg-blue">通知</span>';
                                }
                                else if (d.type === 2) {
                                    return '<span class="layui-badge layui-bg-green">公告</span>';
                                }
                            }
                        },
                        { title: '创建时间', field: 'createTime', width: 160 },
                        { title: '操作', toolbar: '#bar', align: 'right', width: 170 }
                    ]];
                //
                tools.PageTable({ url: '/api/sysNotice/page', cols });
                table.on('toolbar(lsttable)', function (obj) {
                    var id = obj.config.id;
                    var checkStatus = table.checkStatus(id);
                    switch (obj.event) {
                        case 'add':
                            app.add();
                            break;
                    };
                });
                table.on('tool(lsttable)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'edit':
                            app.add(data);
                            break;
                        case 'del':
                            app.del(data.id);
                            break;
                    };
                });
            }();
        //
        app.add = function (data) {
            tools.open({
                area: ['700px', '100%'],
                content: './add',
                    title: '通知公告管理',
                success: function (layero, index, that) {
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    iframeWin.app.data = data
                },
                yes: function (index, layero) {
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    iframeWin.app.save();
                }
            });
        }
        app.del = function (id) {
                var index = layer.confirm('您确定要删除当前通知公告吗？', function () {
                http.post('/api/sysNotice/delete', { id }).then((res) => {
                    if (res.code == 200) {
                        layer.close(index);
                        table.reload('lsttable');
                        toast.success({ message: '删除成功！' });
                    }
                    else {
                        layer.alert('删除失败，错误信息' + res.message, { icon: 2 });
                    }
                })
            });
        }
                })
    </script>
}
